package com.gf.framework.web.swagger;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.spring.web.scanners.ApiListingReferenceScanner;

@Configuration
@EnableOpenApi
public class SwaggerConfiguration {

    @Value("${spring.application.name:unknow}")
    private String applicationName;

    @Value("${spring.auto-returnpack:false}")
    private Boolean autoReturnPack;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(applicationName)
                .description(applicationName + "接口文档。")
                .contact(new Contact("gaofei", "http://localhost", "763196964@qq.com"))
                .version("1.0")
                .build();
    }

    @Bean
    public ApiListingReferenceScanner apiListingReferenceScanner() {
        if (autoReturnPack) {
            return new SwaggerApiListingReferenceScanner();
        }else {
            return new ApiListingReferenceScanner();
        }
    }
}
